﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAccess;
using EPiServer.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PageTypeBuilder;
using TemplateFoundation.Configuration;
using TemplateFoundation.Core;
using TemplateFoundation.Tests.Environment;
using TemplateFoundation.Tests.PageTypes;

namespace TemplateFoundation.Tests.UnitTests.Core
{
    [TestClass]
    public class PageFactoryArticles
    {
        [TestInitialize]
        public void Setup()
        {
            Settings.Instance.NewsContainer = PageStructure.CreateNewsContainer("News");
        }

        [TestCleanup]
        public void Cleanup()
        {
            DataFactory.Instance.Delete(Settings.Instance.NewsContainer, true, AccessLevel.NoAccess);
            Settings.Instance.NewsContainer = null;
        }

        [TestMethod]
        public void GetPublishedArticlesWithMaxCount()
        {
            // Create two published articles and one unpublished one
            PageStructure.CreateArticles(2, true);
            PageStructure.CreateArticles(2, false);

            var articles = PageFactory.Instance.GetArticles(3);

            Assert.IsTrue(articles.Count==2, "Expected 2 articles to be included");

            articles = PageFactory.Instance.GetArticles(3, true);

            Assert.IsTrue(articles.Count==3, "Expected 3 articles to be included");

            PageStructure.CreateArticles(1, true);

            articles = PageFactory.Instance.GetArticles(3);

            Assert.IsTrue(articles.Count==3, "Expected 3 articles to be included");
        }

        [TestMethod]
        public void EnsureExpiredArticlesAreFilteredOut()
        {
            PageStructure.CreateArticles(3, true);

            var articles = PageFactory.Instance.GetArticles(3);

            Assert.IsTrue(articles.Count==3,"Expected 3 articles in total at this point");

            var clone = articles[2].CreateWritableClone();

            clone.StopPublish = DateTime.Now.AddDays(-1);

            DataFactory.Instance.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);

            articles = PageFactory.Instance.GetArticles(3);

            Assert.IsTrue(articles.Count==2, "Expected 2 articles in total at this point because one is expired");

            articles = PageFactory.Instance.GetArticles(3,true);

            Assert.IsTrue(articles.Count==3, "Expected 3 articles in total at this point because unpublished articles are included");
        }

        [TestMethod]
        public void TestGettingArticlesByAuthor()
        {
            var articleCountPerAuthor = 5;

            var articleCount = PageFactory.Instance.GetArticles().Count;

            // Create articles by the first author
            var firstAuthor = PageStructure.CreatePersonPage("John Doe");
            for (int i=0; i<articleCountPerAuthor; i++)
            {
                PageStructure.CreateArticle(DateTime.Now.AddDays(-i), true, firstAuthor.PageLink);
            }

            // Create articles by the second author
            var secondAuthor = PageStructure.CreatePersonPage("Jane Doe");
            for (int i=0; i<articleCountPerAuthor; i++)
            {
                PageStructure.CreateArticle(DateTime.Now.AddDays(-i), true, secondAuthor.PageLink);
            }

            Assert.IsTrue(PageFactory.Instance.GetArticles().Count == articleCount + articleCountPerAuthor*2, "Number of articles does not seem to have increased by " + articleCountPerAuthor*2 + " as expected");

            // Get articles by first author
            var articles = PageFactory.Instance.GetArticlesByAuthor(firstAuthor.PageLink);
            Assert.IsTrue(articles.Count==articleCountPerAuthor, "Unable to retrieve expected number of articles by the first author");

            // Get articles by second author
            articles = PageFactory.Instance.GetArticlesByAuthor(secondAuthor.PageLink);
            Assert.IsTrue(articles.Count==articleCountPerAuthor, "Unable to retrieve expected number of articles by the second author");
        }
    }
}
